home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / help.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  7.5 KB  |  274 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        help.c
  4.  
  5. Purpose:    This module handles displaying the different help windows.
  6.  
  7. \**********************************************************************/
  8.  
  9. #include "graphics.h"            /* must come first because it defines WindowDataHandle */
  10. #include "help.h"
  11. #include "menus.h"
  12. #include "util.h"
  13. #include "program globals.h"
  14.  
  15. int                gNumHelp;
  16. int                gWhichHelp;
  17. int                gLastHelp;
  18.  
  19. /*-----------------------------------------------------------------------------------*/
  20. /* internal stuff for help.c                                                         */
  21.  
  22. void SetupTheHelpWindow(WindowDataHandle theData);
  23. void ShutdownTheHelpWindow(WindowDataHandle theData);
  24. void InitializeTheHelpWindow(WindowDataHandle theData);
  25. void OpenTheHelpWindow(WindowDataHandle theData);
  26. void KeyPressedInHelpWindow(WindowDataHandle theData, unsigned char keyPressed);
  27. void MouseClickedInHelpWindow(WindowDataHandle theData, Point mouseLoc);
  28. void DrawTheHelp(void);
  29. void AddNameToString(Str255 theLine, Str255 theStringToAdd);
  30.  
  31. static    Handle            gHelpIcon[4];
  32.  
  33.  
  34. int HelpWindowDispatch(ExtendedWindowDataHandle theData, int theMessage, unsigned long misc)
  35. {
  36.     unsigned char    theChar;
  37.     Point            thePoint;
  38.     
  39.     switch (theMessage)
  40.     {
  41.         case kUpdate:
  42.             DrawTheHelp();
  43.             return kSuccess;
  44.             break;
  45.         case kKeydown:
  46.             theChar=misc&charCodeMask;
  47.             KeyPressedInHelpWindow(theData, theChar);
  48.             return kSuccess;
  49.             break;
  50.         case kMousedown:
  51.             thePoint.h=(misc>>16)&0x7fff;
  52.             thePoint.v=misc&0x7fff;
  53.             MouseClickedInHelpWindow(theData, thePoint);
  54.             return kSuccess;
  55.             break;
  56.         case kOpen:
  57.             OpenTheHelpWindow(theData);
  58.             return kSuccess;
  59.             break;
  60.         case kInitialize:
  61.             InitializeTheHelpWindow(theData);
  62.             return kSuccess;
  63.             break;
  64.         case kStartup:
  65.             SetupTheHelpWindow(theData);
  66.             return kSuccess;
  67.             break;
  68.         case kShutdown:
  69.             ShutdownTheHelpWindow(theData);
  70.             return kSuccess;
  71.             break;
  72.     }
  73.     
  74.     return kFailure;        /* revert to default processing for all other messages */
  75. }
  76.  
  77. void SetupTheHelpWindow(WindowDataHandle theData)
  78. {
  79.     int                i;
  80.     unsigned char    *helpStr="\pHelp";
  81.     
  82.     for (i=0; i<4; i++)
  83.         gHelpIcon[i]=GetIcon(128+i);        /* get icons from .rsrc file */
  84.     gNumHelp=CountMItems(gHelpMenu);        /* total # of help screens */
  85.     gLastHelp=0;                            /* # of last selected help screen */
  86.     
  87.     (**theData).windowWidth=300;
  88.     (**theData).windowHeight=250;
  89.     (**theData).windowType=noGrowDocProc;    /* document-looking thing */
  90.     (**theData).hasCloseBox=TRUE;
  91.     (**theData).windowBounds.top=50;
  92.     (**theData).windowBounds.left=10;
  93.     Mymemcpy((**(gTheWindowData[kHelp])).windowTitle, helpStr, helpStr[0]+1);
  94. }
  95.  
  96. void ShutdownTheHelpWindow(WindowDataHandle theData)
  97. {
  98.     int            i;
  99.     
  100.     for (i=0; i<4; i++)
  101.         ReleaseResource(gHelpIcon[i]);            /* clean up by disposing icons */
  102. }
  103.  
  104. void InitializeTheHelpWindow(WindowDataHandle theData)
  105. {
  106.     (**theData).initialTopLeft.v=(**theData).windowBounds.top-9;
  107.     (**theData).initialTopLeft.h=(**theData).windowBounds.left;
  108. }
  109.  
  110. void OpenTheHelpWindow(WindowDataHandle theData)
  111. {
  112.     if (gWhichHelp!=gLastHelp)                    /* if new help screen, force update */
  113.         (**theData).offscreenNeedsUpdate=TRUE;    /* of offscreen GWorld/bitmap */
  114. }
  115.  
  116. void KeyPressedInHelpWindow(WindowDataHandle theData, unsigned char keyPressed)
  117. {
  118.     switch (keyPressed)
  119.     {
  120.         case 0x1c:                                        /* left arrow */
  121.             gLastHelp=gWhichHelp;                        /* save last screen # */
  122.             gWhichHelp--;                                /* decrement screen # */
  123.             if (gWhichHelp==0)                            /* wrap around */
  124.                 gWhichHelp=gNumHelp;
  125.             OpenTheWindow((**theData).windowIndex);        /* display new screen */
  126.             break;
  127.         case 0x1d:                                        /* right arrow */
  128.             gLastHelp=gWhichHelp;                        /* save last screen # */
  129.             gWhichHelp++;                                /* increment screen # */
  130.             if (gWhichHelp>gNumHelp)                    /* wraparound */
  131.                 gWhichHelp=1;
  132.             OpenTheWindow((**theData).windowIndex);        /* display new screen */
  133.             break;
  134.     }
  135. }
  136.  
  137. void MouseClickedInHelpWindow(WindowDataHandle theData, Point mouseLoc)
  138. {
  139.     Rect        iconRect;
  140.     Boolean        isHilited;
  141.     
  142.     SetRect(&iconRect, 20, 7, 52, 39);        /* rectangle where left arrow icon is */
  143.     if (PtInRect(mouseLoc, &iconRect))        /* if we hit it... */
  144.     {
  145.         isHilited=FALSE;
  146.         while (StillDown())                    /* track mouse */
  147.         {
  148.             GetMouse(&mouseLoc);            /* returns point in window's local coords */
  149.             if (PtInRect(mouseLoc, &iconRect))    /* if still in icon's rectangle */
  150.             {
  151.                 if (!isHilited)                /* hilight icon if not already hilighted */
  152.                     PlotIcon(&iconRect, gHelpIcon[2]);
  153.                 isHilited=TRUE;                /* so we know */
  154.             }
  155.             else                            /* else we moved outside icon's rectangle */
  156.             {
  157.                 if (isHilited)                /* dehilight if highlighted */
  158.                     PlotIcon(&iconRect, gHelpIcon[0]);
  159.                 isHilited=FALSE;            /* so we know */
  160.             }
  161.         }
  162.         if (isHilited)                        /* if we were still in icon's rect on mouseup */
  163.         {
  164.             gLastHelp=gWhichHelp;            /* save last help screen # */
  165.             gWhichHelp--;                    /* decrement screen # */
  166.             if (gWhichHelp==0)                /* wrap around */
  167.                 gWhichHelp=gNumHelp;
  168.             OpenTheWindow((**theData).windowIndex);        /* display new help screen */
  169.         }
  170.     }
  171.     else    /* do same thing as above, only with the right arrow icon */
  172.     {
  173.         OffsetRect(&iconRect, 228, 0);
  174.         if (PtInRect(mouseLoc, &iconRect))
  175.         {
  176.             isHilited=FALSE;
  177.             while (StillDown())
  178.             {
  179.                 GetMouse(&mouseLoc);
  180.                 if (PtInRect(mouseLoc, &iconRect))
  181.                 {
  182.                     if (!isHilited)
  183.                         PlotIcon(&iconRect, gHelpIcon[3]);
  184.                     isHilited=TRUE;
  185.                 }
  186.                 else
  187.                 {
  188.                     if (isHilited)
  189.                         PlotIcon(&iconRect, gHelpIcon[1]);
  190.                     isHilited=FALSE;
  191.                 }
  192.             }
  193.             if (isHilited)
  194.             {
  195.                 gLastHelp=gWhichHelp;
  196.                 gWhichHelp++;
  197.                 if (gWhichHelp>gNumHelp)
  198.                     gWhichHelp=1;
  199.                 OpenTheWindow((**theData).windowIndex);
  200.             }
  201.         }
  202.     }
  203. }
  204.  
  205. void DrawTheHelp(void)
  206. {
  207.     GrafPtr            curPort;
  208.     int                row;
  209.     Handle            textHandle;
  210.     Str255            theLine;
  211.     unsigned long    pos;
  212.     unsigned long    theSize;
  213.     unsigned char    theChar;
  214.     Rect            iconRect;
  215.     int                theWidth;
  216.     
  217.     GetPort(&curPort);
  218.     EraseRect(&(curPort->portRect));
  219.     
  220.     theWidth=curPort->portRect.right-curPort->portRect.left;
  221.     
  222.     TextFont(geneva);
  223.     TextSize(9);
  224.     row=63;
  225.     textHandle=GetResource('TEXT', 399+gWhichHelp);        /* get text from .rsrc file */
  226.     if (textHandle==0L)        /* if not there, abort */
  227.         return;
  228.     if (*textHandle==0L)    /* if there but SetResLoad=FALSE, load it already! */
  229.         LoadResource(textHandle);
  230.     if (*textHandle==0L)    /* if still no luck, abort */
  231.         return;
  232.     pos=0L;
  233.     theSize=SizeResource(textHandle);    /* size of text */
  234.     while (pos<theSize)
  235.     {
  236.         theLine[0]=0x00;
  237.         /* the beauty of C -- this just gathers characters in theLine (pascal string)
  238.            until (1) the end of the text, or (2) a return character */
  239.         while ((pos<theSize) && ((theChar=*((unsigned char*)(((long)(*textHandle))+(pos++))))!=0x0d))
  240.         {
  241.             if (theChar=='^')        /* ^ character means insert program name */
  242.                 AddNameToString(theLine, APPLICATION_NAME);
  243.             else theLine[++theLine[0]]=theChar;
  244.         }
  245.         MoveTo(8, row);
  246.         DrawString(theLine);        /* draw one line of help */
  247.         row+=12;
  248.     }
  249.     
  250.     ReleaseResource(textHandle);    /* important!  or we'll get memory leaks */
  251.     
  252.     MoveTo(0, 46);
  253.     Line(300, 0);
  254.     
  255.     SetRect(&iconRect, 20, 7, 52, 39);
  256.     PlotIcon(&iconRect, gHelpIcon[0]);        /* plot left arrow icon */
  257.     OffsetRect(&iconRect, 228, 0);
  258.     PlotIcon(&iconRect, gHelpIcon[1]);        /* plot right arrow icon */
  259.     
  260.     GetItem(gHelpMenu, gWhichHelp, theLine);    /* get help screen title from help menu */
  261.     TextFace(bold);
  262.     MoveTo((theWidth-StringWidth(theLine))/2, 26);        /* center it */
  263.     DrawString(theLine);
  264.     TextFace(0);
  265. }
  266.  
  267. void AddNameToString(Str255 theLine, Str255 theStringToAdd)
  268. {
  269.     int            i;
  270.     
  271.     for (i=1; i<=theStringToAdd[0]; i++)    /* insert application name into help string */
  272.         theLine[++theLine[0]]=theStringToAdd[i];
  273. }
  274.